import { ControlType as TYPE } from "@components";
import { Button, Space, message, Popconfirm } from "antd"
import { RULE_REQUIRED } from '@rules'
import { useMainContext } from '@contexts/MainContext'
import { deleteTemplateById } from "@apis/informTemplateApi";

const fields = [
    { name: 'unitCode', label: '归属机构', controlType: TYPE.SELECT, codeKey: 'userAuthUnits' }, 
    { name: 'title', label: '模板标题', controlType: TYPE.INPUT }, 
    // { name:'tempType', label:'模板类型', controlType:TYPE.SELECT, codeKey:'msgTempType' },
    { name: 'status', label: '状态', controlType: TYPE.SELECT, codeKey: 'status' }
]

const del = (row, queryFormRef) => {
  return new Promise(async (resolve, reject) => {
    const {data, error} = await deleteTemplateById(row.msgTempId)
    if (error) return reject(error)
    resolve()
    queryFormRef.current.submit()
    message.success(data)
  })
};

const GetColumns = ( queryFormRef, edit ) => {
    const { isButtomShow } = useMainContext()

    return [
        { title: '归属机构', dataIndex: 'unitName', key: 'unitName', open: true },
        { title: '模板标题', dataIndex: 'title', key: 'title', open: true },
        // { title: '模板类型', dataIndex: 'tempTypeName', key: 'tempTypeName', open: true },
        { title: '模板内容', dataIndex: 'content', key: 'content', open: true },
        { title: '模板状态', dataIndex: 'statusName', key: 'statusName', open: true },
        {
            title: "操作",
            dataIndex: 'operationCol',
            key: 'operationCol',
            open: true,
            fixed: 'right',
            render: (_, record) => {
                return (
                    <Space>
                        {isButtomShow('430102') && <Button type="primary" onClick={() => edit(record)}>编辑</Button>}
                        {isButtomShow('430103') && <Popconfirm title="是否确定删除该区域？" onConfirm={() => del(record, queryFormRef)}>
                          <Button type="primary" danger>删除</Button>
                        </Popconfirm>}
                    </Space>
                )
            }
        }
    ]
}

const ModalFields = () => {
    const { getCode } = useMainContext()
    const newExter = getCode('msgTempTag')?.options.reduce((total, current) => total + current.value+ '，', '')

    return [
        { name:'unitCode', label:'归属机构', controlType:TYPE.SELECT, codeKey:'userAuthUnits', rules: [RULE_REQUIRED]},
        { name:'title', label:'模板标题', controlType:TYPE.INPUT, rules: [RULE_REQUIRED] },
        { 
            name:'content', label:'模板内容', controlType:TYPE.TEXTAREA, style: { height: 150 },
            formitemprops:{rules: [RULE_REQUIRED], extra: "模板内容支持匹配替换字段：" + newExter}
        },
        { name: 'status', label: '状态', controlType: TYPE.SELECT, codeKey: 'status', rules: [RULE_REQUIRED] }
    ]
}

export { fields, GetColumns, ModalFields }